var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
    return typeof e;
} : function(e) {
    return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
}, _base = require("base64"), _md = require("md5"), _md2 = _interopRequireDefault(_md);

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function _defineProperty(e, t, n) {
    return t in e ? Object.defineProperty(e, t, {
        value: n,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : e[t] = n, e;
}

var util = {};

function getQuery(e) {
    var t = [];
    if (-1 != e.indexOf("?")) for (var n = e.split("?")[1].split("&"), i = 0; i < n.length; i++) n[i].split("=")[0] && unescape(n[i].split("=")[1]) && (t[i] = {
        name: n[i].split("=")[0],
        value: unescape(n[i].split("=")[1])
    });
    return t;
}

function getUrlParam(e, t) {
    var n = new RegExp("(^|&)" + t + "=([^&]*)(&|$)"), i = e.split("?")[1].match(n);
    return null != i ? unescape(i[2]) : null;
}

function getSign(e, t, n) {
    var i = require("underscore.js"), a = require("md5.js"), o = "", r = getUrlParam(e, "sign");
    if (r || t && t.sign) return !1;
    if (e && (o = getQuery(e)), t) {
        var s = [];
        for (var u in t) u && t[u] && (s = s.concat({
            name: u,
            value: t[u]
        }));
        o = o.concat(s);
    }
    o = i.sortBy(o, "name"), o = i.uniq(o, !0, "name");
    for (var c = "", f = 0; f < o.length; f++) o[f] && o[f].name && o[f].value && (c += o[f].name + "=" + o[f].value, 
    f < o.length - 1 && (c += "&"));
    return r = a(c + (n = n || getApp().siteInfo.token));
}

util.base64Encode = function(e) {
    return (0, _base.base64_encode)(e);
}, util.base64Decode = function(e) {
    return (0, _base.base64_decode)(e);
}, util.md5 = function(e) {
    return (0, _md2.default)(e);
}, util.url = function(e, t) {
    var n = getApp(), i = n.siteInfo.siteroot + "?i=" + n.siteInfo.uniacid + "&t=" + n.siteInfo.multiid + "&v=" + n.siteInfo.version + "&from=wxapp&";
    if (e && ((e = e.split("/"))[0] && (i += "c=" + e[0] + "&"), e[1] && (i += "a=" + e[1] + "&"), 
    e[2] && (i += "do=" + e[2] + "&")), t && "object" === (void 0 === t ? "undefined" : _typeof(t))) for (var a in t) a && t.hasOwnProperty(a) && t[a] && (i += a + "=" + t[a] + "&");
    return i;
}, util.getSign = function(e, t, n) {
    return getSign(e, t, n);
}, util.request = function(i) {
    require("underscore.js");
    var e, t = require("md5.js"), a = getApp();
    (i = i || {}).cachetime = i.cachetime ? i.cachetime : 0, i.showLoading = void 0 === i.showLoading || i.showLoading;
    var n = wx.getStorageSync("userInfo").sessionid, o = i.url;
    if (-1 == o.indexOf("http://") && -1 == o.indexOf("https://") && (o = util.url(o)), 
    getUrlParam(o, "state") || i.data && i.data.state || !n || (o = o + "&state=we7sid-" + n), 
    !i.data || !i.data.m) {
        var r = getCurrentPages();
        r.length && (r = r[getCurrentPages().length - 1]) && r.__route__ && (o = o + "&m=" + r.__route__.split("/")[0]);
    }
    var s = getSign(o, i.data);
    if (s && (o = o + "&sign=" + s), !o) return !1;
    if (wx.showNavigationBarLoading(), i.showLoading && util.showLoading(), i.cachetime) {
        var u = t(o), c = wx.getStorageSync(u), f = Date.parse(new Date());
        if (c && c.data) {
            if (c.expire > f) return i.complete && "function" == typeof i.complete && i.complete(c), 
            i.success && "function" == typeof i.success && i.success(c), wx.hideLoading(), wx.hideNavigationBarLoading(), 
            !0;
            wx.removeStorageSync(u);
        }
    }
    wx.request((_defineProperty(e = {
        url: o,
        data: i.data ? i.data : {},
        header: i.header ? i.header : {},
        method: i.method ? i.method : "GET"
    }, "header", {
        "content-type": "application/x-www-form-urlencoded"
    }), _defineProperty(e, "success", function(e) {
        if (wx.hideNavigationBarLoading(), wx.hideLoading(), e.data.errno) {
            if ("41009" == e.data.errno) return wx.setStorageSync("userInfo", ""), void util.getUserInfo(function() {
                util.request(i);
            });
            if (i.fail && "function" == typeof i.fail) i.fail(e); else if (e.data.message) {
                if (null != e.data.data && e.data.data.redirect) var t = e.data.data.redirect; else t = "";
                a.util.message(e.data.message, t, "error");
            }
        } else if (i.success && "function" == typeof i.success && i.success(e), i.cachetime) {
            var n = {
                data: e.data,
                expire: f + 1e3 * i.cachetime
            };
            wx.setStorageSync(u, n);
        }
    }), _defineProperty(e, "fail", function(e) {
        wx.hideNavigationBarLoading(), wx.hideLoading();
        var t = require("md5.js")(o), n = wx.getStorageSync(t);
        if (n && n.data) return i.success && "function" == typeof i.success && i.success(n), 
        !0;
        i.fail && "function" == typeof i.fail && i.fail(e);
    }), _defineProperty(e, "complete", function(e) {
        i.complete && "function" == typeof i.complete && i.complete(e);
    }), e));
}, util.getWe7User = function(t, e) {
    var n = wx.getStorageSync("userInfo") || {};
    util.request({
        url: "auth/session/openid",
        data: {
            code: e || ""
        },
        cachetime: 0,
        showLoading: !1,
        success: function(e) {
            e.data.errno || (n.sessionid = e.data.data.sessionid, n.memberInfo = e.data.data.userinfo, 
            wx.setStorageSync("userInfo", n)), "function" == typeof t && t(n);
        }
    });
}, util.upadteUser = function(e, t) {
    var n = wx.getStorageSync("userInfo");
    if (!e) return "function" == typeof t && t(n);
    n.wxInfo = e.userInfo, util.request({
        url: "auth/session/userinfo",
        data: {
            signature: e.signature,
            rawData: e.rawData,
            iv: e.iv,
            encryptedData: e.encryptedData
        },
        method: "POST",
        header: {
            "content-type": "application/x-www-form-urlencoded"
        },
        cachetime: 0,
        success: function(e) {
            e.data.errno || (n.memberInfo = e.data.data, wx.setStorageSync("userInfo", n)), 
            "function" == typeof t && t(n);
        }
    });
}, util.checkSession = function(t) {
    util.request({
        url: "auth/session/check",
        method: "POST",
        cachetime: 0,
        showLoading: !1,
        success: function(e) {
            e.data.errno ? "function" == typeof t.fail && t.fail() : "function" == typeof t.success && t.success();
        },
        fail: function() {
            "function" == typeof t.fail && t.fail();
        }
    });
}, util.getUserInfo = function(t, n) {
    var e = function() {
        wx.login({
            success: function(e) {
                util.getWe7User(function(e) {
                    n ? util.upadteUser(n, function(e) {
                        "function" == typeof t && t(e);
                    }) : wx.canIUse("getUserInfo") ? wx.getUserInfo({
                        withCredentials: !0,
                        success: function(e) {
                            util.upadteUser(e, function(e) {
                                "function" == typeof t && t(e);
                            });
                        },
                        fail: function() {
                            "function" == typeof t && t(e);
                        }
                    }) : "function" == typeof t && t(e);
                }, e.code);
            },
            fail: function() {
                wx.showModal({
                    title: "获取信息失败",
                    content: "请允许授权以便为您提供给服务",
                    success: function(e) {
                        e.confirm && util.getUserInfo();
                    }
                });
            }
        });
    }, i = wx.getStorageSync("userInfo") || {};
    i.sessionid ? util.checkSession({
        success: function() {
            n ? util.upadteUser(n, function(e) {
                "function" == typeof t && t(e);
            }) : "function" == typeof t && t(i);
        },
        fail: function() {
            i.sessionid = "", wx.removeStorageSync("userInfo"), e();
        }
    }) : e();
}, util.navigateBack = function(t) {
    var e = t.delta ? t.delta : 1;
    if (t.data) {
        var n = getCurrentPages(), i = n[n.length - (e + 1)];
        i.pageForResult ? i.pageForResult(t.data) : i.setData(t.data);
    }
    wx.navigateBack({
        delta: e,
        success: function(e) {
            "function" == typeof t.success && t.success(e);
        },
        fail: function(e) {
            "function" == typeof t.fail && t.fail(e);
        },
        complete: function() {
            "function" == typeof t.complete && t.complete();
        }
    });
}, util.footer = function(e) {
    var i = getApp(), a = e;
    i.util.request({
        url: "entry/wxapp/GetTabBar",
        data: {
            m: "bbyscm_card"
        },
        success: function(e) {
            if (1 == e.data.data.tabbar_key) var t = e.data.data; else t = i.tabBar;
            for (var n in t.list) t.list[n].pageUrl = t.list[n].pagePath.replace(/(\?|#)[^"]*/g, "");
            a.setData({
                tabBar: t,
                "tabBar.thisurl": a.__route__
            });
        }
    });
}, util.message = function(e, t, n) {
    if (!e) return !0;
    if ("object" == (void 0 === e ? "undefined" : _typeof(e)) && (t = e.redirect, n = e.type, 
    e = e.title), t) {
        var i = t.substring(0, 9), a = "", o = "";
        "navigate:" == i ? (o = "navigateTo", a = t.substring(9)) : "redirect:" == i ? (o = "redirectTo", 
        a = t.substring(9)) : (a = t, o = "redirectTo");
    }
    n || (n = "success"), "success" == n ? wx.showToast({
        title: e,
        icon: "success",
        duration: 2e3,
        mask: !!a,
        complete: function() {
            a && setTimeout(function() {
                wx[o]({
                    url: a
                });
            }, 1800);
        }
    }) : "error" == n && wx.showModal({
        title: "系统信息",
        content: e,
        showCancel: !1,
        complete: function() {
            a && wx[o]({
                url: a
            });
        }
    });
}, util.user = util.getUserInfo, util.showLoading = function() {
    wx.getStorageSync("isShowLoading") && (wx.hideLoading(), wx.setStorageSync("isShowLoading", !1)), 
    wx.showLoading({
        title: "加载中",
        complete: function() {
            wx.setStorageSync("isShowLoading", !0);
        },
        fail: function() {
            wx.setStorageSync("isShowLoading", !1);
        }
    });
}, util.showImage = function(e) {
    var t = e ? e.currentTarget.dataset.preview : "";
    if (!t) return !1;
    wx.previewImage({
        urls: [ t ]
    });
}, module.exports = util;